package com.easysoft.framework.action;

import com.easysoft.commons.utils.StringUtils;
import com.easysoft.commons.utils.WebConstants;
import com.easysoft.framework.annotation.logs.SystemControllerLog;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.patent.model.PatentQuery;
import com.easysoft.framework.patent.model.PatentView;
import com.easysoft.framework.patent.model.SysPatent;
import com.easysoft.framework.patent.service.PatentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by try on 2017/9/12.
 *
 *  专利申请模块
 */
@RestController
@RequestMapping(value = SystemPatentAction.BASE_VIEW)
public class SystemPatentAction {

    public static final String BASE_VIEW = "/patent";
    private final static Logger logger = LoggerFactory.getLogger(SystemPatentAction.class);

    @Autowired
    @Qualifier("service.PatentService")
    private PatentService patentService;

    @RequestMapping(value = "/listPatent", method = RequestMethod.GET)
    public ModelAndView listPatent(Page page, PatentQuery query, HttpSession session){
        logger.info("Call SystemPatentAction.listPatent({},{})", page.toString(), query.toString());
        List<PatentView> listPatent = patentService.selectPatentByPage(page, query);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/listPatent");
        mv.addObject("page", page);
        mv.addObject("query", query);
        mv.addObject("listPatent", listPatent);
        return mv;
    }

    @RequestMapping(value = "/editPatent", method = RequestMethod.GET)
    public ModelAndView editPatent(String action, @RequestParam(required = false) String id){
        logger.info("Call SystemPatentAction.editPatent({},{})", action, id);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/editPatent");
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(id)) {
                return null;
            }
            return mv;
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(id)) {
                return null;
            }
            SysPatent entity = patentService.selectByPrimaryKey(id);
            if (entity == null) {
                return null;
            }
            mv.addObject("entity", entity);
            return mv;
        }
        return null;
    }

    @RequestMapping(value = "/savePatent", method = RequestMethod.POST)
    public int savePatent(String action, SysPatent patent, HttpSession session){
        logger.info("Call SystemPatentAction.savePatent({},{})", action, patent.toString());
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(patent.getId())) {
                return 0;
            }
            return patentService.addSysPatent(patent);
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(patent.getId())) {
                return 0;
            }
            return patentService.updateSysPatents(patent);
        }
        return 0;
    }

    @RequestMapping(value = "/addSysPatent", method = RequestMethod.POST)
    @SystemControllerLog(description = "新增专利信息")
    public int addSysPatent(SysPatent patent, HttpSession session) {
        logger.info("Call SystemPatentAction.addSysPatent({})", patent.toString());
        return patentService.addSysPatent(patent);
    }
    @RequestMapping(value = "/updateSysPatent", method = RequestMethod.POST)
    public int updateSysPatent(SysPatent patent, HttpSession session) {
        logger.info("Call SystemPatentAction.updateSysPatent()");
        return patentService.updateSysPatents(patent);
    }

    @RequestMapping(value = "/deletePatent", method = RequestMethod.POST)
    @SystemControllerLog(description = "删除专利信息")
    public int deleteSysPatent(String id){
        logger.info("Call SystemPatentAction.deleteSysPatent({})",id);
        return patentService.deleteSysPatent(id);
    }
}
